package com.tea.room.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DateUtils {

    /**
     * 获取当前日期 + i天
     * 
     * @param i
     * @return
     */
    public static String getToDay(int i) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i);
        Date date = cal.getTime();
        String format = sdf.format(date);
        return format;
    }

    /**
     * 指定日期后一天。
     * 
     * @param date
     * @return
     */
    public static String afterday(Date date) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.setTime(date);
        cal.add(Calendar.DATE, +1);
        Date d = cal.getTime();
        String format = sdf.format(d);
        return format;
    }

    /**
     * 获取时间 + i小时
     * 
     * @param i
     * @return
     */
    public static Date getAfterHours(int i) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + i);
        Date date = cal.getTime();
        return date;
    }

    /**
     * 计算两个日期的时间差
     * 
     * @param startTime
     * @param endTime
     */
    public static  Map<String, Long> getTimeDifference(String startTime, String endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Map<String, Long> date = new HashMap<String, Long>();
        try {
            Date start = df.parse(startTime);
            Date end = df.parse(endTime);
            long diff = end.getTime() - start.getTime();// 这样得到的差值是毫秒级别
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            date.put("days", days);
            date.put("hours", hours);
            date.put("minutes", minutes);
            System.out.println("" + days + "天" + hours + "小时" + minutes + "分");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取时间的某一个节点
     * 
     * @param type
     *            Calendar的节点类型 YEAR 1 ； MONTH 2 ；DATE 5 ； HOUR_OF_DAY 11；
     *            MINUTE 12；SECOND 13
     * 
     * @return
     */
    public static int getDateTime(int type) {
        int i = 0;
        Calendar c = Calendar.getInstance();
        if (type == Calendar.YEAR) {
            i = c.get(Calendar.YEAR);
        } else if (type == Calendar.MONTH) {
            i = c.get(Calendar.MONTH);
        } else if (type == Calendar.DATE) {
            i = c.get(Calendar.DATE);
        } else if (type == Calendar.HOUR_OF_DAY) {
            i = c.get(Calendar.HOUR_OF_DAY);
        } else if (type == Calendar.MINUTE) {
            i = c.get(Calendar.MINUTE);
        } else if (type == Calendar.SECOND) {
            i = c.get(Calendar.SECOND);
        }
        return i;
    }

    /**
     * 获取当前时间后25小时的时间节点
     * 
     * @param hour
     *            当前时间的小时
     * @param minute
     *            当前时间的分钟
     * @return
     */
    public static List<String> sortTime(int hour, int minute) {
        List<String> list = getTimeList();
        List<String> newList = new ArrayList<String>();
        int index = 0;
        switch (hour) {
        case 0:
            index = (minute < 30) ? 0 : 1;
            break;
        case 1:
            index = (minute < 30) ? 2 : 3;
            break;
        case 2:
            index = (minute < 30) ? 4 : 5;
            break;
        case 3:
            index = (minute < 30) ? 6 : 7;
            break;
        case 4:
            index = (minute < 30) ? 8 : 9;
            break;
        case 5:
            index = (minute < 30) ? 10 : 11;
            break;
        case 6:
            index = (minute < 30) ? 12 : 13;
            break;
        case 7:
            index = (minute < 30) ? 14 : 15;
            break;
        case 8:
            index = (minute < 30) ? 16 : 17;
            break;
        case 9:
            index = (minute < 30) ? 18 : 19;
            break;
        case 10:
            index = (minute < 30) ? 20 : 21;
            break;
        case 11:
            index = (minute < 30) ? 22 : 23;
            break;
        case 12:
            index = (minute < 30) ? 24 : 25;
            break;
        case 13:
            index = (minute < 30) ? 26 : 27;
            break;
        case 14:
            index = (minute < 30) ? 28 : 29;
            break;
        case 15:
            index = (minute < 30) ? 30 : 31;
            break;
        case 16:
            index = (minute < 30) ? 32 : 33;
            break;
        case 17:
            index = (minute < 30) ? 34 : 35;
            break;
        case 18:
            index = (minute < 30) ? 36 : 37;
            break;
        case 19:
            index = (minute < 30) ? 38 : 39;
            break;
        case 20:
            index = (minute < 30) ? 40 : 41;
            break;
        case 21:
            index = (minute < 30) ? 42 : 43;
            break;
        case 22:
            index = (minute < 30) ? 44 : 45;
            break;
        case 23:
            index = (minute < 30) ? 46 : 47;
            break;
        default:
            index = 0;
            break;
        }
        for (int i = index - 1; i < list.size(); i++) {
            if (index == 0) {
                newList.add(list.get(47));
            } else {
                newList.add(list.get(i));
            }
        }
        for (int i = 0; i < index + 1; i++) {
            if (i == 48) {
                newList.add(list.get(0));
            } else if (i == 49) {
                newList.add(list.get(1));
            } else {
                newList.add(list.get(i));
            }
        }
        return newList;
    }

    /**
     * 获取时间节点list
     * 
     * @return
     */
    public static List<String> getTimeList() {
        ArrayList<String> list = new ArrayList<String>();
        list.add(ConstantUtil.time_00_30);
        list.add(ConstantUtil.time_01_00);
        list.add(ConstantUtil.time_01_30);
        list.add(ConstantUtil.time_02_00);
        list.add(ConstantUtil.time_02_30);
        list.add(ConstantUtil.time_03_00);
        list.add(ConstantUtil.time_03_30);
        list.add(ConstantUtil.time_04_00);
        list.add(ConstantUtil.time_04_30);
        list.add(ConstantUtil.time_05_00);
        list.add(ConstantUtil.time_05_30);
        list.add(ConstantUtil.time_06_00);
        list.add(ConstantUtil.time_06_30);
        list.add(ConstantUtil.time_07_00);
        list.add(ConstantUtil.time_07_30);
        list.add(ConstantUtil.time_08_00);
        list.add(ConstantUtil.time_08_30);
        list.add(ConstantUtil.time_09_00);
        list.add(ConstantUtil.time_09_30);
        list.add(ConstantUtil.time_10_00);
        list.add(ConstantUtil.time_10_30);
        list.add(ConstantUtil.time_11_00);
        list.add(ConstantUtil.time_11_30);
        list.add(ConstantUtil.time_12_00);
        list.add(ConstantUtil.time_12_30);
        list.add(ConstantUtil.time_13_00);
        list.add(ConstantUtil.time_13_30);
        list.add(ConstantUtil.time_14_00);
        list.add(ConstantUtil.time_14_30);
        list.add(ConstantUtil.time_15_00);
        list.add(ConstantUtil.time_15_30);
        list.add(ConstantUtil.time_16_00);
        list.add(ConstantUtil.time_16_30);
        list.add(ConstantUtil.time_17_00);
        list.add(ConstantUtil.time_17_30);
        list.add(ConstantUtil.time_18_00);
        list.add(ConstantUtil.time_18_30);
        list.add(ConstantUtil.time_19_00);
        list.add(ConstantUtil.time_19_30);
        list.add(ConstantUtil.time_20_00);
        list.add(ConstantUtil.time_20_30);
        list.add(ConstantUtil.time_21_00);
        list.add(ConstantUtil.time_21_30);
        list.add(ConstantUtil.time_22_00);
        list.add(ConstantUtil.time_22_30);
        list.add(ConstantUtil.time_23_00);
        list.add(ConstantUtil.time_23_30);
        list.add(ConstantUtil.time_24_00);
        return list;
    }

}
